<script setup lang="ts">
import type { ActionSheetAction } from 'vant'
import { ref } from 'vue'

// import { request } from '@/utils/http'
// request.get('/member/profile')
const showSelectAvatar = ref(false)
const actions = [{ name: '相册' }, { name: '拍照' }, { name: '取消' }]
const onSelect = async (action: ActionSheetAction, index: number) => {
  showSelectAvatar.value = false
  if (index == 0) {
    const photoBase64 = await MKMK.pickerPhoto()
    console.log('点了相册', photoBase64)
  } else if (index == 1) {
    console.log('点了拍照')
    MKMK.cameraPlugin()
  }
}

const showBirth = ref(false)
const currentDate = ref(['2024', '01', '01'])
const minDate = new Date(1900, 0, 1)
const maxDate = new Date(2024, 0, 1)
const confirmBirth = (resDate: any) => {
  console.log(resDate['selectedValues'], (resDate['selectedValues'] as string[]).join(''))
  showBirth.value = false
}

const showArea = ref(false)
const areaList = MKMK.getAreaColumns()
const confirmArea = (resArea: any) => {
  console.log(resArea)
  showArea.value = false
}

const showJob = ref(false)
const columns = [
  { text: '软件工程师', value: '软件工程师' },
  { text: '医生', value: '医生' },
  { text: '教师', value: '教师' },
  { text: '律师', value: '律师' },
  { text: '会计师', value: '会计师' },
  { text: '销售经理', value: '销售经理' },
  { text: '市场营销专员', value: '市场营销专员' },
  { text: '建筑师', value: '建筑师' },
  { text: '护士', value: '护士' },
  { text: '机械工程师', value: '机械工程师' }
]
const onConfirmJob = (res) => {
  // showToast(`当前值: ${selectedValues.join(',')}`)
  console.log('当前值:', res)
  showJob.value = false
}
// const onChange = ({ selectedValues }) => {
//   showToast(`当前值: ${selectedValues.join(',')}`)
// }
const onCancelJob = () => (showJob.value = false)
</script>

<template>
  <div class="profile-edit-page">
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image
        round
        width="100"
        height="100"
        class="avatar-img"
        src="https://foruda.gitee.com/avatar/1716533299289369832/5539886_yjh8866_1716533299.png"
        @click="showSelectAvatar = true"
      >
      </van-image>
      <div class="avatar-btn">
        <span>修改头像</span>
      </div>
    </div>

    <!-- 中间表单部分 -->
    <van-cell-group>
      <van-field label="账号" readonly></van-field>
      <van-field label="昵称" placeholder="请输入昵称"></van-field>
      <van-cell title="性别" class="gender">
        <van-radio-group :icon-size="16">
          <van-radio name="男">男</van-radio>
          <van-radio name="女">女</van-radio>
          <van-radio name="未知">未知</van-radio>
        </van-radio-group>
      </van-cell>
      <van-field
        label="生日"
        @click="showBirth = true"
        readonly
        placeholder="请选择日期"
      ></van-field>
      <!-- 选择生日弹层 -->
      <van-popup v-model:show="showBirth" position="bottom" round>
        <van-date-picker
          v-model="currentDate"
          title="选择日期"
          :min-date="minDate"
          :max-date="maxDate"
          @cancel="showBirth = false"
          @confirm="confirmBirth"
        />
      </van-popup>
      <van-field
        label="所在地"
        readonly
        placeholder="请选择所在地"
        @click="showArea = true"
      ></van-field>
      <!-- 选择地址弹层 -->
      <van-popup v-model:show="showArea" position="bottom" round>
        <van-area
          title="选择地址"
          :area-list="areaList"
          @cancel="showArea = false"
          @confirm="confirmArea"
        />
      </van-popup>

      <van-field label="职业" readonly placeholder="请选择职业" @click="showJob = true"></van-field>
      <!-- 选择职业弹层 -->
      <van-popup v-model:show="showJob" position="bottom" round>
        <van-picker
          title="选择职业"
          :columns="columns"
          @confirm="onConfirmJob"
          @cancel="onCancelJob"
        />
      </van-popup>
    </van-cell-group>

    <!-- 保存 -->
    <div class="submit">
      <van-button type="primary" :block="true" :round="true">保存资料</van-button>
    </div>

    <!-- 修改头像弹层 -->
    <van-action-sheet v-model:show="showSelectAvatar" :actions="actions" @select="onSelect" />
  </div>
</template>

<style lang="css" scoped>
.avatar {
  padding: 30px;
  text-align: center;
  background-color: var(--mk-white);
}

.avatar-img {
  box-shadow: 0 0 5px var(--mk-gray);
}

.avatar-btn {
  color: var(--mk-linear_end);
  margin-top: 10px;
}

.gender ::v-deep(.van-cell__title) {
  width: 100px;
  flex: none;
}

.van-radio-group {
  display: flex;
  justify-content: space-between;
  height: 36px;
  width: 180px;
}

.submit {
  padding: 16px;
}
</style>
